package com.zh.flowable.web;

import com.zh.flowable.domain.dto.TaskDTO;
import com.zh.flowable.support.LoginUserHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

/**
 * 页面视图路由
 *
 * @author
 * @date 2025/8/22
 */
@Controller
public class ViewsRouter {

    @GetMapping("/index")
    public String homeView(Model model) {
        var userId = LoginUserHolder.currentUserId();
        System.out.println("current UserId = " + userId);

        modelSetUserInfo(model);

        return "index";
    }

    @GetMapping("/task/detail")
    public String taskDetail(Model model, @RequestParam("id") Long taskId) {
        modelSetUserInfo(model);

        System.out.println("taskId = " + taskId);

        //每个审批单的内容都不一样
        //应该是有个审批单的模板

        //假设type不同
        var form = switch ((int) (taskId % 2)) {
            case 0 -> "ask_leave";
            case 1 -> "raise_salary";
            default -> "";
        };
        System.out.println(">>> taskDetail form = " + form);
        model.addAttribute("taskForm", form);
        TaskDTO taskDTO = new TaskDTO();
        taskDTO.setId(taskId.toString());
        taskDTO.setCreateTime(new Date());

        model.addAttribute("task", taskDTO);
        return "task_detail";
    }

    @GetMapping("/login")
    public String loginView(Model model) {
        modelSetUserInfo(model);

        return "login";
    }

    void modelSetUserInfo(Model model) {
        var userId = LoginUserHolder.currentUserId();
        var username = LoginUserHolder.currentUsername();
        var userRoles = LoginUserHolder.currentUserRoles();

        model.addAttribute("userId", userId); // 向模板传递数据
        model.addAttribute("username", username); // 向模板传递数据
        model.addAttribute("userRoles", userRoles); // 向模板传递数据

    }
}
